package controller
{
	import alternativa.engine3d.controllers.CameraController;
	import alternativa.engine3d.controllers.WalkController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.engine3d.events.MouseEvent3D;
	import alternativa.engine3d.materials.WireMaterial;
	import alternativa.engine3d.primitives.Box;
	import alternativa.types.Point3D;
	import alternativa.utils.FPS;
	
	import flash.display.Stage;
	import flash.events.Event;
	
	import mx.core.UIComponent;
	
	import view.MiniCube;
	import view.UIControls;

	public class Engine
	{
		private var _dimension:int = 3;
		
		private var _canvas:UIComponent
		private var _scene:Scene3D;
		private var _view:View;
		private var _camera:Camera3D;
		private var _controller:WalkController;
		private var _container:Object3D;
		
		private var _stage:Stage;

		
		
		public function Engine(canvas:UIComponent, stage:Stage)
		{
			_canvas = canvas;
			_stage = stage;
			init();
		}
		
		private var scene:Scene3D;
		//private var view:View;
		private var camera:Camera3D;
		private var cameraController:CameraController;
		private var box:Box;
		
		private var _ui:UIControls;
		
		

		public function init(): void 
		{	
			// Creating scene
			_scene = new Scene3D();
			_scene.root = new Object3D();
			
			_container = new Box(33,33,33,3,3,3);
			//(_container as Box).cloneMaterialToAllSurfaces(new WireMaterial(2,0x000099));
			//_container.coords = new Point3D(-15,-15,-15)
			//_container = new Object3D();
			//_container.x = -15;
			_scene.root.addChild(_container);
			buildCube();
			
			_ui = new UIControls();
			//_scene.root.addChild(_ui);
			
			
			// Adding camera and view
			camera = new Camera3D();
			//camera.x = 100;
			camera.y = -50;
			camera.z = 50;
			//camera.rotationY = 180;
			
			_scene.root.addChild(camera);
			
			_view = new View();
			_view.interactive = true;
			_canvas.addChild(_view);
			_view.camera = camera;


			// Connecting camera controller
			
			cameraController = new CameraController(_stage);
			cameraController.camera = camera;
			cameraController.setDefaultBindings();
			cameraController.checkCollisions = true;
			cameraController.collisionRadius = 20;
			cameraController.lookAt(_container.coords);
			cameraController.controlsEnabled = true;

	
			_controller = new WalkController(_stage,_container); 
	
			// FPS display launch
			FPS.init(_stage);

			_stage.addEventListener(Event.RESIZE, onResize);
			_stage.addEventListener(Event.ENTER_FRAME, render);
			onResize(null);
			
			//_canvas.addEventListener(MouseEvent.CLICK, onClick);
			//_canvas.buttonMode = true;
			//_stage.focus = _canvas	;
			
			//_container.addEventListener(MouseEvent3D.CLICK, onUIPosChange);
		}
		
		private function onClick(e:MouseEvent3D):void
		{
			var mesh:Mesh = e.currentTarget as Mesh;
			_ui.transformation.combine(mesh.transformation);
			var point:Point3D = new Point3D(e.localX, e.localY, e.localZ);
			//_ui.coords = point;
		}
		
		private function onResize(e:Event):void {
			_view.width = _canvas.width;
			_view.height = _canvas.height;
			with(_canvas.graphics)
			{
				beginFill(0xFFFFFF);
				drawRect(0,0,_canvas.width, _canvas.height);
			}
		}
		
		private function render(e:Event):void {
			//cameraController.processInput();
			_controller.processInput();
			_scene.calculate();
		}

		
		private function buildCube():void
		{
			var c:MiniCube;
			var count:int;
			for(var i:int = 0; i < _dimension; i++)
			{
				for(var j:int = 0; j < _dimension; j++)
				{
					for(var q:int = 0; q < _dimension; q++)
					{
						c = new MiniCube();
						count++;
						c.id = count;
						c.coords = new Point3D(q*10.2 - 10,j*10.2 - 10,i*10.2 - 10);
						c.addEventListener(MouseEvent3D.CLICK, onClick);
						_container.addChild(c)
					}
				}
			}
		}
		
		public function get ui():UIControls
		{
			return _ui;
		}
		
		public function get container():Object3D
		{
			return _container;
		}
		
		
	}
}